<template>
  <div class="follow-up-page">
    <el-page-header @back="goBack" content="项目跟进">
      <template #content>
        <span class="project-name">{{ project.projectName }}</span>
        <el-tag>{{ project.status }}</el-tag>
      </template>
    </el-page-header>
    
    <el-card class="follow-up-card">
      <!-- 新增跟进表单 -->
      <el-form :model="form" label-width="80px">
        <el-form-item label="跟进内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :rows="5"
            placeholder="请详细描述本次跟进情况..."
            show-word-limit
            maxlength="500"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFollowUp">提交</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 历史记录 -->
      <el-divider content-position="left">历史跟进记录</el-divider>
      <el-timeline>
        <el-timeline-item
          v-for="record in followRecords"
          :key="record.id"
          :timestamp="formatDate(record.followUpDate)"
          placement="top"
        >
          <el-card>
            <p class="follow-content">{{ record.followUpContent }}</p>
            <div class="follow-meta">
              <span>{{ record.followUpPerson }}</span>
              <span>{{ formatTime(record.createdAt) }}</span>
            </div>
          </el-card>
        </el-timeline-item>
        
        <el-timeline-item v-if="!followRecords.length">
          <el-card>
            <p class="no-data">暂无跟进记录</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import { ElMessage } from 'element-plus'
import axios from 'axios'

const api = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL,
  timeout: 5000
})

const route = useRoute()
const router = useRouter()


const project = ref({})
const form = ref({ content: '' })
const followRecords = ref([])

// 加载项目数据和跟进记录
const loadData = async () => {
  const id = route.query.id
  // 项目信息（若父页已传 query.projectData 可省）
  const { data: p } = await api.get(`/api/projects/${id}`)
  project.value = p


  // 跟进记录（新接口）
  const { data: list } = await api.get(`/api/projects/${id}/follow-ups`)
  followRecords.value = list

}

// 提交跟进记录
const submitFollowUp = async () => {
  if (!form.value.content.trim()) {
    ElMessage.warning('请输入跟进内容')
    return
  }
  const id = route.query.id
  await api.post(`/api/projects/${id}/follow-ups`, {
    content: form.value.content,
    person: '当前登录用户'   // 可空，后端已做空处理
  })
  ElMessage.success('跟进已添加')
  form.value.content = ''
  await loadData()   // 刷新列表
}


// 日期格式化
const formatDate = (isoString) => {
  if (!isoString) return '未知日期'
  try {
    return new Date(isoString).toLocaleDateString('zh-CN')
  } catch (e) {
    console.error('日期格式化错误:', e, isoString)
    return isoString // 返回原始字符串
  }
}

// 时间格式化
const formatTime = (isoString) => {
  if (!isoString) return ''
  try {
    return new Date(isoString).toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch (e) {
    console.error('时间格式化错误:', e, isoString)
    return ''
  }
}

const goBack = () => {
  router.go(-1)
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.follow-up-page {
  padding: 20px;
}

.project-name {
  margin-right: 10px;
  font-size: 16px;
}

.follow-up-card {
  margin-top: 20px;
}

.follow-content {
  line-height: 1.6;
  white-space: pre-line;
}

.follow-meta {
  margin-top: 10px;
  font-size: 12px;
  color: #888;
  display: flex;
  justify-content: space-between;
}

.no-data {
  color: #999;
  text-align: center;
  padding: 20px 0;
}
</style>